<?php
/**
 * @see Alinks_Menu_Abstract
 */
require_once('Alinks/Menu/Abstract.php');

/**
 * Displays the import/export menu
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: ImportExport.php 382 2008-07-03 05:31:10Z sean $
 * @package Alinks_Menu
 */
class Alinks_Menu_ImportExport extends Alinks_Menu_Abstract
{
	/**
	 * The valid export format choices
	 *
	 * @var array
	 */
	protected static $_export_formats = array(
		'xml',
		'csv',
		'excel2000',
		'excelxml'
	);
	
	/**
	 * Inits the menu
	 *
	 * Sets up CSS/JavaScript, and processes any submitted forms.
	 */
	public static function init()
	{
		if (isset($_POST[Alinks_Form::FORM_ALINKS])) {
			self::_processForm();
		}
	}

	/**
	 * Shows the import export menu
	 */
	public static function show()
	{
		include('AlinksMenus/ImportExport.php');
	}
	
	/**
	 * Processes the submitted import/export form
	 */
	protected static function _processForm()
	{
		check_admin_referer(Alinks_Form::NONCE_ACTION);
		if (!isset($_POST[Alinks_Form::FORM_ALINKS])) {
			return;
		}
		
		/**
		 * Make sure a valid form is being submitted
		 */
		if ($_POST[Alinks_Form::FORM_ALINKS] != 'export'
			&& $_POST[Alinks_Form::FORM_ALINKS] != 'import') {
			add_action('admin_notices', array('Alinks_Menu_ImportExport', 'adminNotices'));
			self::$_error_msg = 'Unknown form submitted.';
			return;
		}

		if ($_POST[Alinks_Form::FORM_ALINKS] == 'export') {
			self::_processExport();
		} else if ($_POST[Alinks_Form::FORM_ALINKS] == 'import') {
			self::_processImport();
		} else {
			self::_processAdvancedImport();
		}
	}
	
	/**
	 * Processes the export form
	 */
	protected static function _processExport()
	{
		$format = $_POST['format'];
		if (!in_array($format, self::$_export_formats)) {
			add_action('admin_notices', array('Alinks_Menu_ImportExport', 'adminNotices'));
			self::$_error_msg = 'Unknown export format selected.';
			return;
		}
		
		/**
		 * @see Alinks_ImportExport_Export_Factory
		 */
		require_once('Alinks/ImportExport/Export/Factory.php');
		try {
			$exporter = Alinks_ImportExport_Export_Factory::factory($format, $_POST);
		} catch (Alinks_ImportExport_Export_Exception $e) {
			Alinks_Error::showException($e, Alinks_Error::logException($e));
			return;
		}
		
		try {
			$data = $exporter->export();
		} catch (Alinks_ImportExport_Export_Exception $e) {
			Alinks_Error::showException($e, Alinks_Error::logException($e));
			return;
		}
		
		header('Content-type: ' . $exporter->getMimeType());
		header('Content-Disposition: attachment; filename="alinks-' . date('Y-m-d-H-i-s') . '.' . $exporter->getFileExt() . '"');
		echo $data;
		die();
	}
	
	/**
	 * Processes the import form
	 */
	protected static function _processImport()
	{
		if (empty($_FILES['file']) || empty($_FILES['file']['name'])) {
			add_action('admin_notices', array('Alinks_Menu_ImportExport', 'adminNotices'));
			self::$_error_msg = 'You must upload a file to import.';
			return;
		}
		if ($_FILES['file']['error'] != 0) {
			add_action('admin_notices', array('Alinks_Menu_ImportExport', 'adminNotices'));
			self::$_error_msg = 'It looks like there was an error uploading the file. PHP returned the error code ' . $_FILES['file']['error'];
			return;
		}

		$content = @file_get_contents($_FILES['file']['tmp_name']);
		
		/**
		 * @see Alinks_ImportExport_Export_Factory
		 */
		require_once('Alinks/ImportExport/Import/Factory.php');
		try {
			$importer = Alinks_ImportExport_Import_Factory::factoryFromString($content, $_POST);
		} catch (Alinks_ImportExport_Import_Exception $e) {
			Alinks_Error::showException($e, Alinks_Error::logException($e));
			return;
		}
		
		try {
			$importer->import($content);
		} catch (Alinks_ImportExport_Import_Exception $e) {
			Alinks_Error::showException($e, Alinks_Error::logException($e));
			return;
		}
		
		add_action('admin_notices', array('Alinks_Menu_ImportExport', 'adminNotices'));
		self::$_success_msg = 'Your keyphrases were successfully imported!';
		self::$_success_title = 'Keyphrases Imported';
	}
}